<?php

declare(strict_types=1);

namespace App\Controller\Home\User;

use App\Constants\ErrorCode;
use App\Model\Admin\Admin;
use App\Model\Admin\Roles;
use App\Model\Cooperation;
use App\Model\Distributors;
use App\Model\MainBanner;
use App\Model\User;
use App\Services\SmsService;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;
use RedisException;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use Util\Common;
use Phper666\JWTAuth\JWT;
use Phper666\JWTAuth\Util\JWTUtil;
use App\Services\Wechat;
use App\Controller\AbstractController;


class MainController extends AbstractController
{
    use ResponseMsg;

    #[inject]
    protected User $user;
    #[inject]
    protected MainBanner $mainBanner;

    protected Redis $redis;
    protected Normal $normal;
    protected JWT $jwt;
    protected Wechat $wechat;

    public function __construct()
    {
        $this->jwt = container()->get(JWT::class);
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
        $this->wechat = container()->get(Wechat::class);
    }

    /**获取广告位轮播图
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function banner(RequestInterface $request): ResponseInterface
    {
        $type = $request->input('type');
        $list = $this->mainBanner->where('is_del',Common::DEL_SHOW)
            ->where('type',$type)
            ->orderBy('sort','desc')
            ->get();
        return $this->success($list);
    }

    /**提交代理加盟信息
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function cooperation(RequestInterface $request):ResponseInterface{
        $params = $request->inputs(['phone','name']);
        if(!$params['name']){
            return $this->error(1002,'请填写姓名');
        }
        if(strlen($params['name'])>10){
            return $this->error(1002,'姓名应在10个字符之内');
        }
        if(!$this->normal->validatePhoneNumber($params['phone'])){
            return $this->error(1003,'手机号格式不符合规范');
        }
        if(Cooperation::create($params)){
           return $this->success(msg:'提交成功');
        }
         return $this->error(1004,'操作繁忙 请稍后重试');
    }


}
